package core

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func Login() (*gorm.DB, error) {
	db := fmt.Sprintf(
		"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
		Config.Application.Database.Host,
		Config.Application.Database.Port,
		Config.Application.Database.User,
		Config.Application.Database.Password,
		Config.Application.Database.DBName,
	)
	return gorm.Open(postgres.Open(db), &gorm.Config{})
}

var Database *gorm.DB

func init() {
	var err error
	Database, err = Login()
	if err != nil {
		panic("failed to connect database")
	}
}
